package org.tof.hotel.entity;

import org.apache.log4j.Logger;
import org.tof.hotel.Generator;
import org.tof.hotel.csv.CSVParser;
import org.tof.hotel.csv.CSVWriter;

import java.util.ArrayList;
import java.util.Random;
import java.util.UUID;

/** This class corresponds to the hotel administrator */
public class Administrator extends Person{
    static Logger logger = Logger.getLogger(Administrator.class.getName());
    public static ArrayList<Administrator> administrators = new ArrayList<Administrator>();

    public Administrator(String name, String phone, String email) {
        super(name, phone, email);
        administrators.add(this);
        logger.debug("generated object of class \"Administrator\"");
    }

    // -----methods------ //

    /**
     * Processes specified application form.
     * Looks for suitable room and if such room is available, makes reservation
     *
     * @param application application to process
     * @return <code>true</code> if reservation succeeded; otherwise - <code>false</code>
     */
    public boolean process(Application application) {
        application.setAdministrator(this);
        Room room = Room.findRoom(application);
        if (room != null) {
            new Booking(room, application.getBeginDate(), application.getEndDate());
            return true;
        } else {
            return false;
        }
    }

    public static Administrator getById(UUID id){
        for(Administrator administrator : administrators){
            if(administrator.getId() == id){
                return administrator;
            }
        }
        logger.warn("No Customer with such id:" + id);
        return null;
    }

    // ----- GeneratoR ----- //
    @Override
    public void generate(int num) {
        logger.debug("generating " + num + " objects of class \"Administrator\"");
        for(int i=1; i<=num; i++){
            new Administrator(Generator.names[(new Random()).nextInt(6)], "+380 44 777 77 77", "b.gates@gmail.com");
        }
    }
    // ----- CSVGenerable-generator ----- //
    @Override
    public ArrayList<String> getFields(){
        ArrayList<String> list = new ArrayList<String>();
        ArrayList<String> tmp = new ArrayList<String>();
        for(Administrator a : administrators){
            tmp.clear();
            tmp.add(a.getId().toString());
            tmp.add(a.getName());
            tmp.add(a.getPhone());
            tmp.add(a.getEmail());
            list.add(CSVWriter.getInstance().FieldsToCSVString(tmp));
        }
        return list;
    }

    @Override
    public ArrayList<Class> createInstancesFromCsvFile(String filename) {
        CSVParser parser = new CSVParser();
        ArrayList<String[]> parsedFieldValues = new ArrayList<String[]>(parser.parse(filename));
        ArrayList<Class> generated = new ArrayList<Class>();
        for(int i=1; i < parsedFieldValues.size() ;i++){
            generated.add(new Administrator(parsedFieldValues.get(i)[1],parsedFieldValues.get(i)[2],parsedFieldValues.get(i)[3]).getClass());
        }

        return generated;
    }
}
